package com.lvfuyang.commonproject.controller;

import com.lvfuyang.commonproject.common.Result;
import com.lvfuyang.commonproject.service.CryptoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 加密相关接口控制器
 * RSA密钥对，每次启动服务生成，有效期五分钟
 * SM4密钥，每30分钟刷新
 */
@Slf4j
@RestController
@RequestMapping("/crypto")
public class CryptoController {

    @Autowired
    private CryptoService cryptoService;

    /**
     * 获取RSA公钥（用于前端加密密码）
     */
    @PostMapping("/getPublicKey")
    public Result<Map<String, Object>> getPublicKey() {
        Map<String, Object> result = new HashMap<>();
        result.put("publicKey", cryptoService.getRSAPublicKey());
        return Result.success("获取成功", result);
    }

    /**
     * 获取加密信息（包含时间戳，用于验证时效性）
     */
    @PostMapping("/getCryptoInfo")
    public Result<Map<String, Object>> getCryptoInfo() {
        Map<String, Object> result = new HashMap<>();
        result.put("publicKey", cryptoService.getRSAPublicKey());
        result.put("timestamp", System.currentTimeMillis());
        result.put("expire", 5 * 60 * 1000); // 5分钟有效期
        return Result.success("获取成功", result);
    }
} 